#include <memory>
#include <iostream>
#include <cstddef>
using namespace std;

class TraceHeap
{
	int i;
public:
	TraceHeap (int ii) : i(ii) { }
	int getVal() const { return i; }

	static void * operator new (size_t siz)
	{
		void * p = ::operator new(siz);
		cout<< "Allocating TraceHeap object on the heap "\
			<< "at address " << p << endl;
		return p;
	}

	static void operator delete ( void * p )
	{
		cout<< "Deleting TraceHeap object at address " << p << endl;
		::operator delete(p);
	}
	
};

void TestFun1()
{
	TraceHeap *p = new TraceHeap(100);
	cout<< "In TestFun1: " << p->getVal() << endl;

	if(1) //some error occurred, return earlier before free memory, leading to memory leaks.
		return;
		
	delete p;
}

void TestFun2()
{
	auto_ptr<TraceHeap> p(new TraceHeap(200));
	cout<< "In TestFun2: " << p->getVal() << endl;

	if(1) //some error occurred, return earlier. But not lead to memory leak using auto_ptr.
		return;
}

int main( )
{
	cout<< "-------------------------------------------" << endl;
	TestFun1 ( );
	cout<< "-------------------------------------------" << endl;
	TestFun2 ( );
	cout<< "-------------------------------------------" << endl;

	return 0;
}
